#include "KQM2801A.h"
KQM2801A::KQM2801A(void)
{
}
void KQM2801A::begin(void)
{
  Wire.begin();
}
VOCDATA KQM2801A::get_data(void)
{
  byte data[3];
  byte state = 0;
  byte q = 0;
  byte dat;
  VOCDATA retval;
  Wire.beginTransmission(KQM2801AADDRESS);
  Wire.requestFrom(KQM2801AADDRESS , 4);
  while (Wire.available())
  {
    dat = Wire.read();
    if (dat == 0x5f)
    {
      state = 1;
      data[0] = dat;
      q = 1;
      continue;
    }
    if (state == 1)
    {
      data[q] = dat;
      q++;
    }
    if (q > 3)
    {
      Wire.endTransmission();
      if (data[1] == 0xff && data[2] == 0xff)
      {
        retval.ivalue = -1;
        return retval;
      }
      else if ((byte)(data[0] + data[1] + data[2]) == data[3])
      {
        retval.bvalue[1] = data[1];
        retval.bvalue[0] = data[2];
        retval.ivalue = (retval.ivalue + 5) / 10;
        return retval;
      }
      else
      {
        retval.ivalue = -2;
        return retval;
      }
    }
  }
  retval.ivalue = -3;
  return retval;
}
KQM2801A kqm2801a;
